/**
 * Created by kaola on 16-12-13.
 * White
 */
var express = require('express');
var app = express();
var path = require('path');
var mongo = require('mongodb');
var api = require('./api/controller');
// var routes = require('route');
// var monk = require('monk');

var MongoClient = mongo.MongoClient,
    assert = require('assert');

// var db = monk('mongodb://192.168.1.169:30001,192.168.1.169:30002,192.168.1.169:30003/test?readPreference=secondary&replicaSet=my-mongo-set');

// app.get('/db',api.testDb(db));
// Connection URL


var url ="mongodb://192.168.1.169:30002,192.168.1.169:30001,192.168.1.169:30003/test?readPreference=secondary&replicaSet=my-mongo-set";
//var url = 'mongodb://192.168.1.169:30003/test';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
    console.log(url);
    // assert.equal(null, err);
    // var adminDb = db.admin();
    // adminDb.listenDatabases(function (err, dbs) {
    //     test.equal(null, err);
    //     test.ok(dbs.databases.length > 0);
    //     db.close();
    // })
    assert.equal(null, err);
    console.log("Connected correctly to server");

    // db.close();
});

app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.get('/haha',function (req, res) {
    res.sendfile('./views/index.html');
});


var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});